package com.ddk.en.db;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

/**
 * Created by yanhao on 2017-07-07.
 */
@Slf4j
@Configuration
@MapperScan(basePackages = "com.ddk.en.db.mapper.ddkonline", sqlSessionTemplateRef = "ddkonlineSqlSessionTemplate")
public class DDKOnlineDBConfig {


    public DDKOnlineDBConfig() {
        log.info("Load and init ddkonline mysql server....");
    }


    @Bean(name = "ddkonlineDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.ddkonline")
    public DataSource dataSource() {
        PoolProperties p = new PoolProperties();
        p.setJmxEnabled(true);
        p.setTestWhileIdle(true);
        p.setTestOnBorrow(true);
        p.setValidationQuery("SELECT 1");
        p.setTestOnReturn(false);
        p.setValidationInterval(30000);
        p.setTimeBetweenEvictionRunsMillis(30000);
        p.setMaxActive(200);
        p.setInitialSize(20);
        p.setMaxWait(10000);
        p.setRemoveAbandonedTimeout(180);
        p.setMinEvictableIdleTimeMillis(30000);
        p.setMinIdle(10);
        p.setLogAbandoned(true);
        p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;" +
                "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
        DataSource dataSource = new DataSource();
        dataSource.setPoolProperties(p);
        return dataSource;
    }


    @Bean(name = "ddkonlineSqlSessionFactoryBean")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("ddkonlineDataSource") javax.sql.DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/ddkonline/*.xml"));
        log.info("<========== Mysql ddkonline DataSource ==========>: " + dataSource.toString());
        return bean.getObject();
    }

    @Bean(name = "ddkonlineTransactionManager")
    public DataSourceTransactionManager testTransactionManager(@Qualifier("ddkonlineDataSource") javax.sql.DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "ddkonlineSqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("ddkonlineSqlSessionFactoryBean") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }


}

